﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using store.Areas.Admin.Models;

namespace store.Areas.Admin.Controllers
{
    public class ModulesController : Controller
    {
        private readonly storeEntities _db = new storeEntities();
        //
        // GET: /Admin/Modules/

        public ActionResult CreateModulesProduct()
        {
            var createModulesProductModel = new CreateModulesProductModel()
                {
                    DisplayProduct = 20,
                    Active = true,
                };
            var postionList = new Dictionary<string, string> { { "", "Chọn vị trí hiển thị" }, { "top", "Trên" }, { "center", "Giữa" }, { "right", "Phải" }, { "bottom", "Dưới" }, { "left", "Trái" } };
            var selectListPostion = new SelectList(postionList, "Key", "Value");
            ViewBag.selectListPostion = selectListPostion;
            return View(createModulesProductModel);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult CreateModulesProduct(CreateModulesProductModel model)
        {
            if (ModelState.IsValid)
            {
                var options = "<Option CategoryID='" + model.CaregoryId + "' Display='" + model.DisplayProduct + "'></Option>";

                var modules = new Module()
                {
                    Title = model.Title,
                    ModulesType = "Product",
                    Options = options,
                    Postion = model.Postion,
                    SortNumber = model.SortNummber,
                    Active = model.Active,
                    ModifiedDate = DateTime.Now.ToLocalTime()
                };
                _db.Modules.Add(modules);
                _db.SaveChanges();
                if (model.OptionSubmit == "Lưu và Thêm mới")
                {
                    return RedirectToAction("CreateModulesProduct");
                }
                return RedirectToAction("CreateModulesProduct");
            }
            return View(model);
        }

    }
}
